package com.ecoolex.hotel.saas.api.graphql;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSON;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.PresaleCoupon;
import com.ecoolex.hotel.saas.entity.PresaleCouponCategory;
import com.ecoolex.hotel.saas.ws.OperationService;
import com.ecoolex.hotel.saas.ws.PresaleCouponService;
import com.google.common.collect.Lists;

@GraphQL2Module(type = Type.User, description = "预售券")
public class PresaleCouponGraphql {
	
	@Autowired
	private StorageService storageService;

	@Reference
	private OperationService operationService;
	
	@Reference
	private PresaleCouponService presaleCouponService;
	
	@TypeField(type = PresaleCoupon.class, field = "commissionAmt", description = { "销售员可分佣金额" })
	public BigDecimal commissionAmt(@Source PresaleCoupon source, ConsumerMemberAuthority user) {
		user.assertLogin();
		return presaleCouponService.salesUserCommissionAmt(source.getPreCouponId(), source.getStoreId(), user.userId());
	}
	
	@TypeField(type = PresaleCouponCategory.class, field = "image", description = { "分类图片" })
	public StorageInfo image(@Source PresaleCouponCategory source) {
		return storageService.get(source.getImage());
	}
	
	@TypeField(type = PresaleCoupon.class, field = "coverImage", description = { "封面图" })
	public StorageInfo coverImage(@Source PresaleCoupon source) {
		return storageService.get(source.getCoverImage());
	}
	
	@TypeField(type = PresaleCoupon.class, field = "headImage", description = { "头图" })
	public List<StorageInfo> headImage(@Source PresaleCoupon source) {
		if(Check.notNullOrEmpty(source.getHeadImage())) {
			return JSON.parseArray(source.getHeadImage(), String.class).stream()
				.map(storageService::get)
				.filter(Check::notNull)
				.collect(Collectors.toList());
		}
		return Lists.newArrayList();
	}
	
	@QueryField(field =  "presaleCouponCategoryList", description = "预售券分类列表")
	public Pagination<PresaleCouponCategory> query(@NonNull Integer storeId, @NonNull Page page){
		return operationService.queryPresaleCouponCategoryForUser(storeId, null, page);
	}
	
	@QueryField(field =  "presaleCouponList", description = "预售券列表")
	public Pagination<PresaleCoupon> queryPresaleCoupon(String name,@NonNull Integer storeId, Integer categoryId, @NonNull Page page){
		return presaleCouponService.queryPresaleCouponForUser(name, storeId, categoryId, page);
	}
	
	@QueryField(field =  "presaleCoupon", description = "预售券")
	public PresaleCoupon getPresaleCoupon(@NonNull Integer preCouponId) {
		return presaleCouponService.getPresaleCouponForUser(preCouponId);
	}
}
